<?php
class TrambvtvsController extends AppController {

	var $name = 'Trambvtvs';
	var $helpers = array('Html', 'Form');
	var $paginate = array('limit' =>10, 'page' => 1, 'order' =>array('Trambvtv.TEN_TR'=>'asc'));


	//doan code de xet quyen
	function beforeFilter(){
        parent::beforeFilter();
        $curUser = $this->Auth->user();
        if($curUser['User']['ROLE']!='admin'){
            $this->Session->setFlash('Bạn phải đăng nhập bằng quyền ADMIN để sử dụng chức năng này');
            $this->redirect('/pages');
        }
    }





	function index() {
		$this->Trambvtv->recursive = 2;
		$this->set('trambvtvs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Trambvtv.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('trambvtv', $this->Trambvtv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Trambvtv->create();
			if ($this->Trambvtv->save($this->data)) {
				$this->Session->setFlash(__('Thông tin trạm BVTV đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin Trạm Bảo Vệ TT chưa được lưu. Xin bạn vui lòng làm lại', true));
			}
		}
		
			//$loaiDts = $this->Doituong->LoaiDt->find('list',array('fields'=>array('LoaiDt.id','TEN_LOAI')));
		$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list',array('fields'=>array('ChicucBvtv.id','TEN_CC')));
		//$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list');
		$this->set(compact('chicucBvtvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Trambvtv', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Trambvtv->save($this->data)) {
				$this->Session->setFlash(__('Thông tin trạm BVTV đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin Trạm Bảo Vệ TT chưa được lưu. Xin bạn vui lòng làm lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Trambvtv->read(null, $id);
		}
		
		$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list',array('fields'=>array('ChicucBvtv.id','TEN_CC')));
		//$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list');
		$this->set(compact('chicucBvtvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Trambvtv', true));
			$this->redirect(array('action'=>'index'));
		}
		
		//rang buoc
		$Canbo=$this->Trambvtv->Canbo->find('first',array('conditions'=>array('Canbo.trambvtv_id'=>$id)));
		$Bayden=$this->Trambvtv->Bayden->find('first',array('conditions'=>array('Bayden.trambvtv_id'=>$id)));
		$Nongho=$this->Trambvtv->Nongho->find('first',array('conditions'=>array('Nongho.trambvtv_id'=>$id)));
		$Sotay=$this->Trambvtv->Sotay->find('first',array('conditions'=>array('Sotay.trambvtv_id'=>$id)));
		if(			$Canbo['Canbo']['id']!=null || 
					$Bayden['Bayden']['id']!=null ||
					$Nongho['Nongho']['id']!=null ||
					$Sotay['Sotay']['id']!=null  )   {
			$this->Session->setFlash(__('Bạn không thể xóa Trạm BVTV này vì sẽ làm sai các dữ liệu có liên quan', true));
			$this->redirect(array('action'=>'index'));
		}
		
		
		
		if ($this->Trambvtv->del($id)) {
			$this->Session->setFlash(__('Đã XÓA xong', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function tinh_index() {
		$this->Trambvtv->recursive = 0;
		$this->set('trambvtvs', $this->paginate());
	}

	function tinh_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Trambvtv.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('trambvtv', $this->Trambvtv->read(null, $id));
	}

	function tinh_add() {
		if (!empty($this->data)) {
			$this->Trambvtv->create();
			if ($this->Trambvtv->save($this->data)) {
				$this->Session->setFlash(__('The Trambvtv has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Trambvtv could not be saved. Please, try again.', true));
			}
		}
		$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list');
		$this->set(compact('chicucBvtvs'));
	}

	function tinh_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Trambvtv', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Trambvtv->save($this->data)) {
				$this->Session->setFlash(__('The Trambvtv has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Trambvtv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Trambvtv->read(null, $id);
		}
		$chicucBvtvs = $this->Trambvtv->ChicucBvtv->find('list');
		$this->set(compact('chicucBvtvs'));
	}

	function tinh_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Trambvtv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Trambvtv->del($id)) {
			$this->Session->setFlash(__('Trambvtv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>